package com.yilian.xlmusic.android.view.adapter;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.yilian.xlmusic.android.R;
import com.yilian.xlmusic.android.utils.MeasureUnitTranUtil;
import com.yilian.xlmusic.android.utils.RecycleViewPager;
import com.yilian.xlmusic.android.utils.music.DiffDemoCallback;
import com.yilian.xlmusic.android.view.bean.MusicPlayerBean;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: hao
 * @Date 2017/10/27 15:32
 * @Description ViewPager适配器
 * 避免fragment切换反复DestroyView和CreateView
 **/

public class MusicViewPagerAdapter extends PagerAdapter {

    private List<String> list;
    private Context context;
    private RecycleViewPager viewPager;
    private int fix_height;
    private List<MusicPlayerBean.DataBean> music_list;
    private FoudMusicAdapter MainAdapter;

    public MusicViewPagerAdapter(Context mcontext, List<String> mlist, List<MusicPlayerBean.DataBean> dataBeans, RecycleViewPager mviewPager, FoudMusicAdapter foudMusicAdapter) {
        super();
        MainAdapter = foudMusicAdapter;
        context = mcontext;
        viewPager = mviewPager;
        music_list = dataBeans;
        list = mlist;
    }


    /**
     * 页面宽度所占ViewPager测量宽度的权重比例，默认为1
     */
    @Override
    public float getPageWidth(int position) {
        if (position == list.size() - 1) {
            return (float) 1;
        } else {
            return (float) 0.94;
        }
    }

    /**
     * 用于Adapter清空数据
     *
     * @param object
     * @return
     */
    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }


    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public void destroyItem(@NotNull ViewGroup container, int position, @NotNull Object object) {
//        super.destroyItem(container, position, object);
        container.removeView((View) object);
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        View view = LayoutInflater.from(context).inflate(R.layout.adapter_recycleview_layout, container, false);
        RecyclerView recyclerView = view.findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));



        ItemRecycleViewAdapter itemRecycleViewAdapter;
        List<MusicPlayerBean.DataBean> categoryBeans = new ArrayList<>();
        if (position == list.size() - 1) {//最后一页特殊处理
            itemRecycleViewAdapter = new ItemRecycleViewAdapter(R.layout.music_player_item2, categoryBeans, true);
            categoryBeans.addAll(music_list.subList((position * 3), music_list.size()));

        } else {
            itemRecycleViewAdapter = new ItemRecycleViewAdapter(R.layout.music_player_item, categoryBeans,false);
            categoryBeans.addAll(music_list.subList(position * 3, 3 + (position * 3)));
        }
        itemRecycleViewAdapter.setDiffCallback(new DiffDemoCallback());
        recyclerView.setAdapter(itemRecycleViewAdapter);
        container.addView(view);

        /**固定recyclerView的高度*/
        recyclerView.post(new Runnable() { //将修改高度的代码放到RecyclerView最后面执行，让RecyclerView先测量完毕
            @Override
            public void run() {
                if (recyclerView.getAdapter() == null || recyclerView.getAdapter().getItemCount() == 0) { //不固定高度
                    return;
                }
                View view = recyclerView.getChildAt(0);
                if (view == null) {
                    return;
                }
                int view_height = view.getHeight() * 3;//固定成3个item的高度
                //Log.e("固定好的高度：", "" + view_height);
                LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) recyclerView.getLayoutParams();
                layoutParams.height = view_height;
                recyclerView.setLayoutParams(layoutParams);
//                if (fix_height == 0) {
//                    fix_height = view_height;
//                    setViewPagerHeight(viewPager);
//                }
            }
        });

        /**固定recyclerView的高度*/
        setViewPagerHeight(viewPager);
        return view;
    }

    /**
     * 根据RecycleView的高度设置ViewPager的高度
     */
    private void setViewPagerHeight(ViewPager viewPager) {
        if (fix_height == 0) {
            viewPager.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                @Override
                public boolean onPreDraw() {
                    viewPager.getViewTreeObserver().removeOnPreDrawListener(this);
                    ViewGroup.LayoutParams layoutParams = viewPager.getLayoutParams();
                    layoutParams.height = MeasureUnitTranUtil.dip2px(context, 260);
                    //Log.e("11水电费发生的方式", "" + MeasureUnitTranUtil.dip2px(context, 280));
                    viewPager.setLayoutParams(layoutParams);
                    return true;
                }
            });
        } else {
            viewPager.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                @Override
                public boolean onPreDraw() {
                    viewPager.getViewTreeObserver().removeOnPreDrawListener(this);
                    ViewGroup.LayoutParams layoutParams = viewPager.getLayoutParams();
                    layoutParams.height = fix_height;
                    //Log.e("11水电费发生的方式", "" + fix_height);
                    viewPager.setLayoutParams(layoutParams);
                    return true;
                }
            });
        }

    }


}
